package com.dindang.checkline.mvp.presenter;

import android.app.Application;

import com.alibaba.fastjson.JSON;
import com.dindang.checkline.R;
import com.dindang.checkline.mvp.model.entity.BaseResponse;
import com.dindang.checkline.mvp.model.entity.LineDetailEntity;
import com.dindang.checkline.mvp.model.entity.TaskDetail;
import com.dindang.checkline.mvp.ui.activity.adapter.costom.LineDetailAdapter;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;

import javax.inject.Inject;

import com.dindang.checkline.mvp.contract.LineDetailContract;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.RxLifecycleUtils;
import com.longxi.gamer_base.BaseAppConst;
import com.longxi.gamer_base.MyApplication;
import com.longxi.gamer_base.dialog.WaitDialog;

import java.util.ArrayList;
import java.util.List;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 03/26/2019 15:36
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class LineDetailPresenter extends BasePresenter<LineDetailContract.Model, LineDetailContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;
    private List<LineDetailEntity.OfficerCountBean> mList = new ArrayList<>();
    private LineDetailAdapter mAdapter;
    private WaitDialog mWaitDialog;
    private LineDetailEntity.LineDetailParam mParam = new LineDetailEntity.LineDetailParam();
    private String mState="0";
    private TaskDetail mDataTask = new TaskDetail();

    public void init() {
        mWaitDialog = new WaitDialog(mRootView.getActivity());
//        EventBus.getDefault().register(this);
        mDataTask= JSON.parseObject(mRootView.getActivity().getIntent().getStringExtra("data"),TaskDetail.class);
        initAdapter();
        initListener();
        getData();
    }

    private void initListener() {
        //点击需要上传的缓存巡检项
        mRootView.getRlupddate().setOnClickListener(v -> {
     if(mRootView.gettvUpdataeNum().getText().toString().equals("0")){
         ArmsUtils.makeText(mRootView.getActivity(),"没有需要上传的数据");
     }else {//开始上传

     }
        });
    }

    //获取巡检信息
    private void getData() {
        //以下是真实调接口
        mWaitDialog.showWaitingDialog(ArmsUtils.getString(mApplication, R.string.data_load));
        mParam.id = mDataTask.getRecord().getL_id()+"";
        mModel.queryLineDetail(mParam)
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(2, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .doOnSubscribe(disposable -> {

                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mWaitDialog.hideWaitingDialog();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<BaseResponse<LineDetailEntity>>(mErrorHandler) {
                    @Override
                    public void onNext(BaseResponse<LineDetailEntity> response) {
                        if(response.getStatus().equals(BaseAppConst.OUT_TOKEN)){
                            MyApplication.getInstance().gotoLoginActivity();
                        }
                      if(response.isSuccess()){
                          mList.addAll(response.getData().getOfficerCount());
                          mAdapter.setFirst(response.getData().getRecord());
                          mAdapter.notifyDataSetChanged();
                      }
                    }
                });
    }

    /**
     * 初始化适配器
     */
    private void initAdapter() {
        mAdapter = new LineDetailAdapter(mList);
        mRootView.getRecyclerView().setAdapter(mAdapter);
    }

    @Inject
    public LineDetailPresenter(LineDetailContract.Model model, LineDetailContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
